package net.cyue.web.easyquery.core.db.data;

import net.cyue.web.easyquery.core.db.QueryType;
import net.cyue.web.easyquery.core.db.SQLType;

public class SQLPageInfo extends SQLInfo {

    QueryPage queryPage;

    public SQLPageInfo(String sql, int page, int pageSize) {
        this(sql, new QueryPage(page, pageSize));
    }
    public SQLPageInfo(String sql, int page, int pageSize, boolean doCount) {
        this(sql, new QueryPage(page, pageSize, doCount));
    }
    public SQLPageInfo(String sql, QueryPage queryPage) {
        super(sql, SQLType.QUERY);
        this.queryType = QueryType.QUERY_PAGE;
        this.queryPage = queryPage;
    }

    /**
     * 从 SQL 中创建 SQLInfo，自动判断类型，性能较差，尽量不用
     * @param sql 语句
     * @param page 页码
     * @param pageSize 页大小
     * @param doCount 是否执行 count
     * @return SQLPageInfo
     */
    public static SQLPageInfo fromSQL(String sql, int page, int pageSize, boolean doCount) {
        return new SQLPageInfo(sql, page, pageSize, doCount);
    }
    public static SQLPageInfo fromSQLInfo(SQLInfo sqlInfo, int page, int pageSize, boolean doCount) {
        if (sqlInfo == null) {
            return null;
        }
        if (sqlInfo.getSQLType() != SQLType.QUERY) {
            throw new RuntimeException("SQLInfo is not a query SQL");
        }
        return new SQLPageInfo(sqlInfo.getSql(), page, pageSize, doCount);
    }

    public QueryPage getQueryPage() {
        return this.queryPage;
    }
    public int getPage() {
        return this.queryPage.getPage();
    }
    public int getPageSize() {
        return this.queryPage.getPageSize();
    }
    public boolean ifDoCount() {
        return this.queryPage.ifDoCount();
    }
}
